<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/10
 * Time: 21:58
 */
namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Request;
use app\lnadmin\model\admin\Article as ArticleModel;
use app\lnadmin\model\admin\Keyword as Words;
use app\lnadmin\model\admin\Click;
use app\lnadmin\model\admin\Notice;
use app\lnadmin\model\admin\Image as ImageModel;
use app\lnadmin\model\admin\Navbar;
use app\lnadmin\controller\Upload;
/**
 * 文章
 * Class Article
 * @package app\controller\admin
 */
class Article extends InitController
{
    /**
     * 页面
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/10 22:41
     */
    public function list()
    {
        return $this->fetch('article');
    }

     //列表详情
    public function index_json($limit='15')
    {
        $param = $this->request->param();
        $article = new ArticleModel();
        
        if (isset($param['subtitle'])) {
            $article = $article->whereLike('title','%'.$param['subtitle'].'%');
        }  
        // 时间搜索
        if (!empty($param['create_time'])) {
            // 切割时间 * 注意两边的空格 ' - '
            $time = explode(' - ', $param['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $article = $article->whereBetweenTime('create_time', $start, $end);
        }
        $list = $article->order(['id'=>'desc'])->paginate($limit);
        
        if (!empty($list)) {
            // 获取图片
            foreach ($list as $k => $v) {
                $v['image'] = ImageModel::image($v['id'], Upload::TYPE_ARTICLE);
                      if(!empty($v['cat_id'])){
                $list[$k]['count'] = $article->count();
                $list[$k]['cat_name'] = Navbar::findNavbar($v['cat_id'])['cat_name'];
                  }
             }
        }

        $this->result($list);
    }
    //// 通用搜索
   public function search_json()
    {
        $keywords = $this->request->get('search');
        
        if(!empty($keywords)){
            $map=[['title','like','%'.$keywords.'%']];
        
        }
        else{
            $map=true;
        }
        $data = ArticleModel::where($map)->order('id desc')       
        ->paginate(['list_rows' => 8, 'query' => request()->param()])
            ->each(function ($item, $key) {
                $item['image'] = ImageModel::image($item['id'], Upload::TYPE_ARTICLE);
                $item['flag'] = ArticleModel::flag($item['flag']);
                 $count = Notice::where('article_id', $item['id'])->count();
                $item['count'] = isset($count)?$count:0;
             $item['cat_name'] = Navbar::findNavbar($item['cat_id'])['cat_name'];
                return $item;
            });
            add_admin_log('search_json', '数据');// 记录行为日志
        return view('/user/search', ['data' => $data, 'keywords' => $keywords]);
    }
    /**
     * 新增
     * @return array
     * @author: MK
     * @Time: 2020/4/10 22:12
     */
    public function add()
    {
        if ($this->request->isPost()) {
        $param = $this->request->param();
        if (!$param['flag'] || !$param['cat_id']) return $this->error('错误，所属栏目或者标志未选择');
         /*过滤关键字*/
        $model = Words::keyWordCheck();
        $item = [];

         foreach($model as $k=>$v)
         {
            $item[$k] = $v['word'];
         }
        
        $param['content'] = str_replace($item,"<font color='#ff0000;'>{此处关键词违规}</font>",$param['content']);
        $data = ArticleModel::add($param);

        if ($data == true) {
        add_admin_log('add', '文章');// 记录行为日志
        return $this->success('', '', '新增成功');
           }
    }
      
        $data = \app\lnadmin\model\admin\Navbar::navbar();
        $tree = $this->dropTree($data,0,0);
      
        return $this->fetch('saveArticle', ['name' => $tree]);
    }

    /**
     * 编辑
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/10 22:12
     */
   
    // 编辑
    public function edit()
    {
        if ($this->request->isPost()) {
        $param = $this->request->param();
        $edit = ArticleModel::edit($param);
        return $edit ? $this->saveSuccess() : $this->error();
         }
        
        $data = \app\lnadmin\model\admin\Navbar::navbar();
        $tree = $this->dropTree($data,0,0);
        return $this->fetch('saveArticle', [
            'list' => ArticleModel::where('id', input('id'))->find(), 
            'name' => $tree, 
            'img'  => ImageModel::where(['table_id' => input('id'), 'types' => upload::TYPE_ARTICLE])->find()
        ]);
    }
  
    /**
     * 删除
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/10 22:13
     */
  
  // 删除
    public function del()
    {
        
       if ($this->request->isPost()) { 
            $param = $this->request->param();
            $result = ArticleModel::destroy($param['id']);
            if ($result == true) {
            add_admin_log('del', '删除文章');// 记录行为日志
                return $this->success('', '', '删除成功');
            }
        }
    }
     // 全部启用 // 全部禁用where(['status'=>0])->
    public function statuslog()
    {
        if ($this->request->isPost()) {
            $uid = $this->request->get('uid');
          if($uid==0){
                ArticleModel::where(['status'=>1])->update(['status'=>$uid]);
            }else{
                ArticleModel::where(['status'=>0])->update(['status'=>$uid]);
            }
         return $this->success(true, 0, '操作成功');
       }
    }
 
   
}